package com.mttest.hotwords.comteoller;

import com.alibaba.fastjson.JSONArray;
import com.mttest.hotwords.common.ServerResponse;
import com.mttest.hotwords.entity.HotWordInfo;
import com.mttest.hotwords.service.ManageService;
import com.mttest.hotwords.service.ScheduledTaskService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author sunqy
 * @description 后台管理Controller
 * @create 2020-08-31 22:42
 */
@Controller
@RequestMapping("/manage")
public class ManageController {

    @Resource
    private ManageService manageService;

    @Resource
    private ScheduledTaskService scheduledTaskService;

    //更新热词的定时任务
    public static final String TASK_KEY = "hotWordKey";

    //查询热词及对应次数


    /**
     * 运营配置热词 保存
     * @param request
     * @return
     */
    @PostMapping("/setKeyWords")
    public ServerResponse setKeyWords(HttpServletRequest request){
        //获取前端入参，前端配置的热词list
        List<HotWordInfo> hotWordInfos = JSONArray.parseArray(request.getParameter("hotWordInfos"),HotWordInfo.class);
        //将前端传过来的数据插入到数据库里面
        int count = manageService.setHotWordInfoForeach(hotWordInfos);
        // TODO 热点数据redis重建缓存，来不及开发

        return count > 0 ? ServerResponse.createBySuccess() : ServerResponse.createByErrorMessage("保存失败!");
    }

    /**
     * 运营配置热词更新频率及热词数量
     * @param request
     * @return
     */
    @PostMapping("/setFrequencyAndNum")
    public ServerResponse setFrequencyAndNum(HttpServletRequest request){

        // TODO 这边去更新数据库里面的配置表，这块还来不及开发

        //配置完重启定时任务
        scheduledTaskService.restart(TASK_KEY);
        return ServerResponse.createBySuccess();
    }

}
